﻿KISSY.use("validation, fgt/auth, fgt/ui", function (S, Validation, Auth, UI) {
    S.ready(function (S) {
        var $ = S.all,
            J = jQuery,
            form = $("#J_RVCreate"),
            VALID_EFFECT = "Simple",
            VALID_STYLE = "s_b",
            validator = new Validation(form[0], { warn: VALID_EFFECT, style: VALID_STYLE }),
            submitBtn = $("#J_CreateSubmit"),
            madeWrap = $("#J_RVMadeWrap"),
            madeByInput = $("#rv_made_by"),
            popbox = UI.getPopbox();

        validator.add("#rv_type",
        {
            required: [true, "Model & Style is required."]
        });

        validator.add("#rv_model_style",
        {
            required: [true, "Model & Style is required."],
            maxLength: [100, true, "At most 100 characters."]
        });

        validator.add("#rv_made_year",
        {
            required: [true, "Year is required."]
        });

        validator.add("#rv_made_by",
        {
            required: [true, "Made by is required."]
        });

        validator.add("#rv_sleeps",
        {
            required: [true, "Sleeps is required."]
        });

        validator.add("#rv_length",
        {
            required: [true, "Length is required."],
            regex: [/^\d+(\.\d+)?$/, 'The format of length is incorrect.'],
            min: [1, "Length must be more than {} FT."]
        });

        validator.add("#rv_zip",
        {
            required: [true, "zip is required."],
            zip: ["Please input 5 digit number for zip."]
        });

        validator.add("#rv_availability",
        {
            required: [true, "Availability is required."]
        });

        madeByInput.on("change", function (event) {
            event.halt();
            var self = $(this);
            if (self.val() == 0) {
                madeWrap.show();
            }
            else {
                madeWrap.hide();
            }
        });

        function submitRV() {
            try {
                var paramStr = S.ajax.serialize(form),
                    resultCode = FGT.core.constant.jsonResultCode,
                    failureText = "Save failure, please try again later.",
                    delay = 1000;

                J.ajax({
                    url: "/rv/create",
                    type: "POST",
                    data: paramStr,
                    beforeSend: function (jqXHR, settings) {
                        submitBtn.prop("disabled", true);
                        popbox.show("Saving");
                    },
                    success: function (data, textStatus, jqXHR) {
                        if (data.Result === resultCode.SUCCESS) {
                            popbox.hide();
                            window.location.href = data.Content.redirectUrl;
                        }
                        else {
                            var jsonData = data.Content;
                            if (S.isString(jsonData)) {
                                popbox.show(jsonData, delay);
                            }
                            else if (S.isArray(jsonData)) {
                                S.each(jsonData, function (item) {
                                    var target = $(item.id);
                                    if (target.attr("data-msgbox")) {
                                        var label = $(target.attr("data-msgbox"));
                                        if (label) {
                                            label.text(item.msg);
                                            label.removeClass("ok tip error");
                                            label.addClass("error");
                                            label.show();
                                        }
                                    }
                                });
                                popbox.hide();
                            }
                            submitBtn.prop("disabled", false);
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        submitBtn.prop("disabled", false);
                        popbox.show(failureText, delay);
                    }
                });
            }
            catch (e) {
                popbox.show(failureText, delay);
                submitBtn.prop("disabled", false);
            }
        }

        submitBtn.on("click", function (event) {
            event.halt();
            var self = this,
                b = validator.isValid();
            if (b === true) {
                submitRV();
            }
        });
    });
});